package me.wangxuan.pat.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

import lombok.Data;
import lombok.EqualsAndHashCode;
import me.wangxuan.pat.entity.Api;
import me.wangxuan.pat.entity.Project;
@Data
@EqualsAndHashCode(callSuper = false)
public class ApiManageDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private DefaultTableModel tableModel;
	private JTable recordTable;
	private List<Api> records;
	private JButton addButton;
	private JButton deleteButton;
	private JButton updateButton;
	private List<Project> projects;

	public ApiManageDialog(JFrame owner, List<Api> records, List<Project> projects) {
		super(owner, "账号管理", true);
		// 设置窗口大小
		setSize(800, 600);
		// 设置窗口布局
		setLayout(new BorderLayout());
		this.records = records;
		this.projects = projects;

		// 初始化表格模型
		tableModel = new DefaultTableModel(new Object[] { "接口ID", "接口名称", "所属项目", "接口地址", "参数配置" }, 0) {
			private static final long serialVersionUID = 1L;

			// 覆盖 isCellEditable 方法
			@Override
			public boolean isCellEditable(int row, int column) {
				// 返回 false 表示该单元格不可编辑
				// 例如，这里我们将所有单元格设为不可编辑
				return false; // 或者根据需要返回 true/false
			}
		};

		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new FlowLayout());
		List<Project> pList = new ArrayList<>();
		pList.addAll(projects);
		Project pAll = new Project();
		pAll.setProjectId(0L);
		pAll.setProjectName("全部");
		pList.add(0, pAll);

		JComboBox<Project> projectBox = new JComboBox<>(pList.toArray(new Project[0]));
		searchPanel.add(projectBox);
		JButton searchButton = new JButton("查询");
		searchPanel.add(searchButton);
		add(searchPanel, BorderLayout.NORTH);

		for (Api a : records) {
			tableModel.addRow(new Object[] { a.getApiId(), a.getApiName(), a.getProject().getProjectName(), a.getUrl(),
					a.getParams() });
		}
		// 初始化表格
		recordTable = new JTable(tableModel);
		// 创建表格面板
		JScrollPane scrollPane = new JScrollPane(recordTable);
		add(scrollPane, BorderLayout.CENTER);
		// 创建按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		// 添加按钮：增加、删除、修改、查询
		addButton = new JButton("增加");
		deleteButton = new JButton("删除");
		updateButton = new JButton("修改");

		// 将按钮添加到按钮面板
		buttonPanel.add(addButton);
		buttonPanel.add(deleteButton);
		buttonPanel.add(updateButton);

		// 将按钮面板添加到窗口底部
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public class AddRecordForm extends JDialog {
		private static final long serialVersionUID = 1L;
		// 定义标签和文本框的宽度
		int labelWidth = 100;
		int textFieldWidth = 250;
		private JPanel formPanel;
		private JComboBox<Project> projectBox;
		private JTextField apiName;
		private JTextField url;
		private JComboBox<String> mBox;
		private JComboBox<String> cBox;
		private RSyntaxTextArea params;
		private Api result;

		public AddRecordForm(JFrame owner, List<Project> projects, Api a) {
			super(owner, "增加记录", true); // 模态对话框
			this.result = a;
			// 设置窗口大小
			setSize(850, 600);
			// 设置窗口布局
			setLayout(new BorderLayout());

			// 创建表单面板
			formPanel = new JPanel();
			formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS)); // 使用 BoxLayout.Y_AXIS 实现垂直排列

			JPanel panel1 = new JPanel();
			panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel label1 = new JLabel("所属项目:");
			label1.setPreferredSize(new Dimension(labelWidth, label1.getPreferredSize().height));
			panel1.add(label1);
			projectBox = new JComboBox<Project>(projects.toArray(new Project[0]));
			projectBox.setPreferredSize(new Dimension(textFieldWidth, projectBox.getPreferredSize().height));
			panel1.add(projectBox);
			formPanel.add(panel1);

			JPanel panel2 = new JPanel();
			panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel label2 = new JLabel("接口名称:");
			label2.setPreferredSize(new Dimension(labelWidth, label2.getPreferredSize().height));
			panel2.add(label2);
			apiName = new JTextField();
			apiName.setPreferredSize(new Dimension(textFieldWidth, apiName.getPreferredSize().height));
			panel2.add(apiName);
			formPanel.add(panel2);

			JPanel panel3 = new JPanel();
			panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel label3 = new JLabel("接口URL:");
			label3.setPreferredSize(new Dimension(labelWidth, label3.getPreferredSize().height));
			panel3.add(label3);
			url = new JTextField();
			url.setPreferredSize(new Dimension(textFieldWidth, url.getPreferredSize().height));
			panel3.add(url);
			formPanel.add(panel3);
			
			JPanel panelm = new JPanel();
			panelm.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel labelm = new JLabel("请求方法:");
			labelm.setPreferredSize(new Dimension(labelWidth, labelm.getPreferredSize().height));
			panelm.add(labelm);
			mBox = new JComboBox<String>(new String[]{"POST","GET"});
			mBox.setPreferredSize(new Dimension(textFieldWidth, mBox.getPreferredSize().height));
			panelm.add(mBox);
			formPanel.add(panelm);
			
			JPanel panelc = new JPanel();
			panelc.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel labelc = new JLabel("ContentType:");
			labelc.setPreferredSize(new Dimension(labelWidth, labelc.getPreferredSize().height));
			panelc.add(labelc);
			cBox = new JComboBox<String>(new String[]{"application/x-www-form-urlencoded", "application/json","application/xml","text/plain","text/xml","text/html","application/octet-stream","text/event-stream","multipart/form-data"});
			cBox.setPreferredSize(new Dimension(textFieldWidth, cBox.getPreferredSize().height));
			panelc.add(cBox);
			formPanel.add(panelc);

			JPanel panel4 = new JPanel();
			panel4.setLayout(new FlowLayout(FlowLayout.CENTER));
			JLabel label4 = new JLabel("接口参数:");
			label4.setPreferredSize(new Dimension(labelWidth, label4.getPreferredSize().height));
			panel4.add(label4);

			params = new RSyntaxTextArea(15, 35);
			params.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
			params.setCodeFoldingEnabled(false);// 关闭折叠
			// 创建一个滚动面板
			RTextScrollPane paramsScrollPane = new RTextScrollPane(params);
			paramsScrollPane.setLineNumbersEnabled(false);// 关闭行号显示
			paramsScrollPane.setPreferredSize(new Dimension(textFieldWidth, params.getPreferredSize().height));
			panel4.add(paramsScrollPane);
			
			formPanel.add(panel4);

			Project selected = (Project) projectBox.getSelectedItem();
			if (selected == null) {
				selected = projectBox.getItemAt(0);
			}
			
			if(result != null) {
				projectBox.setSelectedItem(result.getProject());
				apiName.setText(result.getApiName());
				url.setText(result.getUrl());
				mBox.setSelectedItem(result.getMethod());
				cBox.setSelectedItem(result.getContentType());
				params.setText(result.getParams());
			}

			// 创建按钮面板
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout());
			// 创建“确定”按钮
			JButton okButton = new JButton("确定");
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					Project selected = (Project) projectBox.getSelectedItem();
					// 获取用户输入的数据
					String aName = apiName.getText().trim();
					String aurl = url.getText().trim();
					String paramsCfg = params.getText().trim();

					// 验证输入是否为空
					if (aName.isEmpty() || aurl.isEmpty() || paramsCfg.isEmpty()) {
						JOptionPane.showMessageDialog(AddRecordForm.this, "请填写所有字段！");
						return;
					}

					// 创建新记录
					if (result == null) {
						result = new Api();
					}
					result.setProject(selected);
					result.setApiName(aName);
					result.setUrl(aurl);
					result.setMethod((String)mBox.getSelectedItem());
					result.setContentType((String)cBox.getSelectedItem());
					result.setParams(paramsCfg);
					// 关闭对话框
					dispose();

				}
			});

			// 创建“取消”按钮
			JButton cancelButton = new JButton("取消");
			cancelButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 清空输入框
					apiName.setText("");
					url.setText("");
					params.setText("");
					// 关闭对话框
					dispose();
				}
			});

			// 将按钮添加到按钮面板
			buttonPanel.add(okButton);
			buttonPanel.add(cancelButton);

			// 将表单面板和按钮面板添加到对话框
			add(formPanel, BorderLayout.CENTER);
			add(buttonPanel, BorderLayout.SOUTH);
		}
		
		public Api getRecord() {
			return this.result;
		}
	}

}
